package com.lgkj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.lgkj.R;
import com.lgkj.bean.HairSalonBean;
import com.lgkj.utils.CommonUtils;
import com.lgkj.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/25 0025.
 */

public class HairSalonAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<HairSalonBean.DataBean> salonBean;
    private MyClickListener mListener;

    public void setClick(MyClickListener listener) {
        this.mListener = listener;
    }

    //自定义接口，用于回调按钮点击事件到Activity
    public interface MyClickListener {
        public void clickListener(View v);
    }

    public HairSalonAdapter(Context context) {
        this.context = context;
        this.salonBean = new ArrayList<>();
    }

    public void setData(List<HairSalonBean.DataBean> dataBeans) {
        if (dataBeans != null) {
            this.salonBean = dataBeans;
        }
    }

    @Override
    public int getCount() {
        return salonBean.size() + 1;
    }

    @Override
    public HairSalonBean.DataBean getItem(int i) {
        return salonBean.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_price, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (((MyGridView) viewGroup).isOnMeasure) {
            //如果是onMeasure调用的就立即返回
            return view;
        }
        if (salonBean.size() == 0) {
            viewHolder.ivUser.setImageResource(R.mipmap.hair_salon_add);
            viewHolder.ivDel.setVisibility(View.GONE);
        } else {
            if (i < salonBean.size()) {
                viewHolder.ivDel.setOnClickListener(this);
                viewHolder.ivDel.setTag(i);
                viewHolder.ivDel.setVisibility(View.VISIBLE);
                CommonUtils.LoadImage(context, viewHolder.ivUser, salonBean.get(i).getHairstyle_img(), 0);
            } else {
                viewHolder.ivDel.setVisibility(View.GONE);
                viewHolder.ivUser.setImageResource(R.mipmap.hair_salon_add);
            }
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.iv_user)
        ImageView ivUser;
        @BindView(R.id.iv_del)
        ImageView ivDel;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }


    }

    //响应按钮点击事件,调用子定义接口，并传入View
    @Override
    public void onClick(View v) {
        mListener.clickListener(v);
    }
}
